package com.hup.utils.swing.customView.viewTable2;

import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.swing.customView.viewTable2.ViewTableColumn.VSummaryHandler;
import lombok.extern.slf4j.Slf4j;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicTableHeaderUI;
import javax.swing.table.JTableHeader;
import java.awt.Component;
import java.awt.Dimension;
import java.util.List;

/**
 * @author hugan
 * @date 2024/6/9
 */
@Slf4j
class ViewTableHeader extends BasicTableHeaderUI {

    private final List<VColumnInfo> columns;
    private final MyHeaderRenderer headerRenderer;

    /**
     * 初始化表头
     *
     * @param columns 这是visibleColumns!!
     */
    protected static <R> void initTableHeader(ViewTable<R> table, List<VColumnInfo> columns) {
        ViewTableHeader tableHeaderUI = new ViewTableHeader(table, columns);
        JTableHeader tableHeader = table.getTableHeader();
        tableHeader.setUI(tableHeaderUI);

        //渲染器: 实现[每列文本对齐, 统计行]效果
        tableHeader.setDefaultRenderer(tableHeaderUI.headerRenderer);

        table.getTableHeader().setReorderingAllowed(false);//不允许拖拽列
        table.setAutoCreateRowSorter(true);//内置排序功能

        //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//会导致,拉伸列宽时ui有bug
        table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);//内置自动列宽

        //设置自定义列宽
        table.resetColumnWidth();

        //处理统计行 高度; 要使用table.getColumns()
        boolean hasSummary = table.getColumns().stream().anyMatch(c -> c.getSummaryHandler() != null);
        if (hasSummary) {
            int rowHeight = (int) (table.getRowHeight() * LookThemeUtil.getScreenScale());
            table.getTableHeader().setPreferredSize(new Dimension(table.getTableHeader().getPreferredSize().width, rowHeight * 3));
        }

        //表头菜单
        table.setHeaderMenu(new VHeaderMenu<>(table));
    }

    private ViewTableHeader(JTable table, List<VColumnInfo> columns) {
        this.columns = columns;
        headerRenderer = new MyHeaderRenderer();
    }

    /**
     * @return nullable, 获取[列表+统计]信息文本
     */
    @SuppressWarnings("unchecked")
    public static <R> String getSummaryText(VColumnInfo config, List<R> data) {
        VSummaryHandler<R> handler = (VSummaryHandler<R>) config.getSummaryHandler();
        if (handler == null) return null;

        String align;
        if (config.getHeaderAlignment() == SwingConstants.LEFT) {
            align = "left";
        } else if (config.getHeaderAlignment() == SwingConstants.RIGHT) {
            align = "right";
        } else {
            align = "center";
        }
        return "<html>"
                + "<p align='" + align + "'>"
                + config.getName() + "<br/>"
                + handler.get(data) + ""
                + "</p>"
                + "</html>";
    }

    /**
     * 渲染器: 实现[每列文本对齐, 统计行]效果
     */
    class MyHeaderRenderer extends DefaultTableCellHeaderRenderer {
        @SuppressWarnings("MagicConstant")
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component res = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            //log.info("[{},{}]={}", row, column, value);

            VColumnInfo columnInfo = columns.get(column);

            //每列 单独的[水平对齐方式]
            int headerAlignment = columnInfo.getHeaderAlignment();
            setHorizontalAlignment(headerAlignment);

            //统计行
            if (columnInfo.getSummaryText() != null) {
                ((JLabel) res).setText(columnInfo.getSummaryText());
            }

            if (columnInfo.getHeaderTooltip() != null) {
                ((JLabel) res).setToolTipText(columnInfo.getHeaderTooltip());
            } else {
                ((JLabel) res).setToolTipText(null);
            }

            return res;
        }
    }

}
